package handler

import (
	"github.com/gin-gonic/gin"
	"pic-share-sys/model"
	"pic-share-sys/pkg"
)

func Register(c *gin.Context) {
	Username := c.PostForm("Username")
	Password := pkg.Md5(c.PostForm("Password"))
	data := model.User{
		Username: Username,
		Password: Password,
	}
	if len(Username) != 11 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "手机号必须是11位",
		})
		return
	}
	if len(Username) < 6 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码必须是字母和数字混合超过6位",
		})
		return
	}
	var user model.User
	err := model.DB.Debug().Where("Username =?", Username).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户名已存在",
		})
		return
	}
	if user.ID > 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户已存在",
		})
		return
	}
	err = model.DB.Debug().Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "注册失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "注册成功",
		"data": user,
	})
	return
}
